
import VueRouter from 'vue-router'
import About from '../pages/About'
import Home from '../pages/Home'
import Message from '../pages/Message'
import News from '../pages/News'
import Detail from '../pages/Detail'

const router = new VueRouter({
    routers:[
        {
            path:"/about",
            component:About,
        },
        {
            path:"/home",
            component:Home,
            children:[
                {
                    path:"message",
                    name:"xiaoxi",
                    component:Message,
                    children:[
                        {
                            name:"xiangqing",
                            path:"detail/:id/:title",
                            component:Detail,
                            props(route){
                                return {
                                    id:route.params.id,
                                    title:route.params.title,
                                }
                            },
                        }
                    ]
                },
                {
                    path:"news",
                    name:"xinwen",
                    component:News
                },
            ]
        },
    ]
})

// 全局前置路由守卫
// 初始化的时候被调用
// 每次切换之前被调用
router.beforeEach((to,from,next)=>{
    console.log(to,from,next)
    //权限验证完毕后，要调用next函数去执行后面的内容
    // next()
    // if(to.path === "/home/message" || to.path === "/home/news"){
    // 权限控制就在这里控制    
    if(to.name === "xinwen" || to.name === "xiaoxi"){
        if(localStorage.getItem("token")==="zzzzzzzzzzzzzzz"){
            next()
        }else{
            alert("无权查看")
        }
    }else{
        next()
    }
})
export default router